import {defineStore} from 'pinia'
import {pictureApi} from '@/api'
import type {PictureQueryDTO} from '@/types/dto/picture'
import type {PictureVO} from '@/types/vo/picture'
import type {PageVO} from '@/types/common'

interface PictureState {
    currentPicture: PictureVO | null
    pictureList: PictureVO[]
    pagination: {
        current: number
        size: number
        total: number
        pages: number
    }
    loading: boolean
}

export const usePictureStore = defineStore('picture', {
    state: (): PictureState => ({
        currentPicture: null,
        pictureList: [],
        pagination: {
            current: 1,
            size: 12,
            total: 0,
            pages: 0,
        },
        loading: false,
    }),

    getters: {
        hasPictures: (state) => state.pictureList.length > 0,
    },

    actions: {
        /**
         * 获取图片列表
         */
        async fetchPictures(params: PictureQueryDTO) {
            try {
                this.loading = true
                const result: PageVO<PictureVO> = await pictureApi.list(params)
                this.pictureList = result.records
                this.pagination = {
                    current: result.current,
                    size: result.size,
                    total: result.total,
                    pages: result.pages,
                }
                return result
            } finally {
                this.loading = false
            }
        },

        /**
         * 获取图片详情
         */
        async fetchPictureById(id: number) {
            try {
                this.loading = true
                const picture = await pictureApi.getById(id)
                this.currentPicture = picture
                return picture
            } finally {
                this.loading = false
            }
        },

        /**
         * 上传图片
         */
        async uploadPicture(payload: any) {
            try {
                this.loading = true
                const picture = await pictureApi.upload(payload)
                return picture
            } finally {
                this.loading = false
            }
        },

        /**
         * 更新图片信息
         */
        async updatePicture(id: number, payload: any) {
            try {
                this.loading = true
                const picture = await pictureApi.update(id, payload)
                // 如果当前图片是被更新的图片，更新当前图片信息
                if (this.currentPicture?.id === id) {
                    this.currentPicture = picture
                }
                // 更新列表中的图片信息
                const index = this.pictureList.findIndex(p => p.id === id)
                if (index !== -1) {
                    this.pictureList[index] = picture
                }
                return picture
            } finally {
                this.loading = false
            }
        },

        /**
         * 删除图片
         */
        async deletePicture(id: number) {
            try {
                this.loading = true
                await pictureApi.delete(id)
                // 从列表中移除
                this.pictureList = this.pictureList.filter(p => p.id !== id)
                // 如果是当前图片，清空当前图片
                if (this.currentPicture?.id === id) {
                    this.currentPicture = null
                }
            } finally {
                this.loading = false
            }
        },

        /**
         * 分享图片
         */
        async sharePicture(id: number) {
            return await pictureApi.share(id)
        },

        /**
         * 清空状态
         */
        clearState() {
            this.currentPicture = null
            this.pictureList = []
            this.pagination = {
                current: 1,
                size: 12,
                total: 0,
                pages: 0,
            }
        },
    },
})




